<template>
    <el-form :model="form" label-width="120px">
        <el-form-item label="用户名" required>
            <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" required>
            <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item label="选择登录身份">
            <el-radio-group v-model="form.identify">
                <el-radio-button label="个人用户" />
                <el-radio-button label="企业用户" />
            </el-radio-group>
        </el-form-item>


        <el-form-item>
            <el-button type="primary" @click="onSubmit">登录</el-button>
            <el-button @click="$emit('close')">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { reactive, defineEmits } from 'vue';
import request from '@/api';
import { ElMessage } from 'element-plus';
import { useStore } from 'vuex';
const store = useStore();
const emit = defineEmits(['close']);
const form = reactive({
    username: '',
    password: '',
    identify: '个人用户',
});

const onSubmit = async () => {
    let res = await request('/login', {
        method: 'post',
        data: {
            username: form.username,
            password: form.password,
            type: form.identify == '个人用户' ? 1 : 2,
        },
    });
    if (res.success) {
        ElMessage.success('登录成功！');
        store.commit('login', res.data);
        emit('close');
    } else {
        ElMessage.error(res.msg);
    }
};
</script>
